package yunquan.wang.modules.base.web;

import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import yunquan.wang.base.service.BaseService;
import yunquan.wang.base.web.BaseController;
import yunquan.wang.modules.base.entity.RolesAuthority;
import yunquan.wang.modules.base.entity.Rolesinfo;
import yunquan.wang.modules.base.entity.RulesInfo;
import yunquan.wang.modules.base.service.RolesinfoService;
import yunquan.wang.modules.base.service.RulesInfoService;

 
@Controller
@RequestMapping("/core/rolesinfo")
public class RolesinfoController extends BaseController<Rolesinfo>{
	@Resource
	private RolesinfoService rolesinfoService;
 
	@Resource(name="rulesInfoService")
	private RulesInfoService rulesInfoService;
 
	/*
	 * 默认列表显示和排序搜索查找
	 */
	@RequestMapping(value = "/list.do")
	public ModelAndView list(HttpServletRequest request) {
		return baseList();
	}

	/*
	 * 默认保存方法
	 */
	@RequestMapping(value = "/save.do")
	public ModelAndView save(HttpServletRequest request,
			@Valid Rolesinfo entity, BindingResult bindingResult, String authIds) {
		entity = rolesinfoService.save(entity, authIds);
		ModelAndView mv = new ModelAndView("redirect:/core/rolesinfo/edit.do?id="+entity.getId());
		return mv;
	}

	@RequestMapping(value = "/edit.do")
	public ModelAndView edit(String id) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("entity", rolesinfoService.get(id));
        mv.addObject("rulesinfo", rulesInfoService.findAll());
		return mv;
	}
 
	/*
	 * 默认查看
	 */
	@RequestMapping(value = "/view.do")
	public ModelAndView view(String id) {
		return baseView();
	}

	@RequestMapping(value = "/rulesinfo.do")
	public ModelAndView rulesinfo(HttpServletRequest request)
	{
		String id=request.getParameter("id");
		
		Rolesinfo entity= rolesinfoService.get(id);
		for(RolesAuthority ra:entity.getRolesAuthority())
		{
		 String authId=	ra.getAuthority().getId();
		 String[] rules=request.getParameterValues(authId+"Rules");
		 Set<RulesInfo>	ruleSet= ra.getRulesInfos();
		 ruleSet.clear();
		 if(rules!=null)
		 {
		 for(String rule:rules)
		 {
			 RulesInfo ri=null;
			 if(StringUtils.isNotBlank(rule))
			 {
				 ri=new RulesInfo();
				 ri.setId(rule);
				 ruleSet.add(ri);
			 }
		 }
		 }
		}
		rolesinfoService.updateRulesinfo(entity);
		ModelAndView mv = new ModelAndView("redirect:list.do");
		return mv;
	}
	
	/*
	 * 默认输入方法
	 */
	@RequestMapping(value = "/input.do")
	public ModelAndView input(String id) {
		ModelAndView mv = new ModelAndView();
		Rolesinfo entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = rolesinfoService.get(id);
			String authIds="";
			String authName="";
				for(RolesAuthority ra:entity.getRolesAuthority())
				{	authIds+=ra.getAuthority().getId()+",";
				 authName+=ra.getAuthority().getAuthinfo()+",";
				}
				if(authIds.length()>1)
				{
					authIds=authIds.substring(0, authIds.length()-1);
					authName=authName.substring(0, authName.length()-1);
				}
				mv.addObject("authIds", authIds);
				mv.addObject("authName", authName);
		} else {
			entity = new Rolesinfo();
		}
		mv.addObject(ENTITY, entity);
		return mv;
	}

	/*
	 * 默认删除方法
	 */
	@RequestMapping(value = "/delete.do")
	public  ModelAndView delete(String id) {
		return baseDelete();
	}

	@Override
	public BaseService<Rolesinfo> getService() {
		return rolesinfoService;
	}
}
